package org.wmine.data;

import java.util.ArrayList;
import java.util.Iterator;

import javax.print.DocFlavor.STRING;
/**
 * @author admin
 *
 */
public class DataSet implements TableResultSet {
	
	/**
	 * @return Returns the number of attributes in a dataset
	 */
	Table table;
	
	public static int  curserPosition = -1;
	
	public String getTableName(){
		return table.getNameTable();
		
	}
	public int getAttributeCount() {
		
//		table = table.getInstance();
		return table.getNoOfAttributes();		
	}
	
	/**
	 * @return Returns the iterator for accessing the attributes
	 */
	public Iterator<Attribute> getAttributeIterator() {
				return table.getAttributeList().iterator();		
	}
	
	/**
	 * @param index Index for the attribute
	 * @return Returns the Attribute at the specified index
	 */
	Attribute getAttribute(int index) throws ArrayIndexOutOfBoundsException {
		ArrayList<Attribute> attributeCollection = new ArrayList<Attribute>(0);
		attributeCollection= table.getAttributeList();
		return attributeCollection.get(index);
	}
	
	/**
	 * @return returns integer that is no of tuples in a table 
	 */
	public int getTupleCount(){	
		return table.getNoOfTuples();		
	}
	
	/** 
	 * @return returns iterators of string type to read all the tuples
	 * all the values are accessed as strings 
	 */
	public TableResultSet getTableResultSet() {
		return (TableResultSet)this;		
	}
	
	public boolean reset(){
		curserPosition = -1;
		return true;		
	}
	
	
	public boolean hasNext() {
	 
		 
		ArrayList <Tuple> tupleList = table.getTupleList();
		
		if(curserPosition<tupleList.size()-1){
			curserPosition ++;
			return true;
		}
		else
		return false;
	}
	
	public String getString(int indexAttribute) throws InvalidCursorPositionException {
		
		String resultAttributeValue;
		resultAttributeValue = table.getTuplesAttributeValue(curserPosition, indexAttribute);
		return resultAttributeValue;
	}
	
	public String getString(String attributeName) throws InvalidCursorPositionException {
		int attributeIndex = 0;
		String resultAttributeValue;  
		attributeIndex = table.getAttributeIndex(attributeName);
	//	System.out.println("In Dataset : cursor of table :"+  curserPosition);
		resultAttributeValue = table.getTuplesAttributeValue(curserPosition, attributeIndex);
		return resultAttributeValue;
	}
	
	public void setDataReader(AbstractDataReader dr) {
		table = dr.getTable();
	}
}
